<?php
/**
 * Created by PhpStorm.
 * 商家商品管理
 * User: caicm
 * Date: 2017/6/17
 * Time: 11:27
 */

namespace WMApi\Controller;
use WMApi\Model\WmGoodsModel;
use WMApi\Model\WmStoreCustomizeMenuModel;
use WMApi\Model\WmStoreModel;
use WMApi\Model\WmUnitModel;
use WMApi\Service\AttrService;
use WMApi\Service\CategoryService;
use WMApi\Service\SellerGoodsService;

class SellerGoodsController extends SellerBaseController {

    /**
     * 商品分类列表
     * Created By caicm
     * @param int $is_return
     * @return mixed
     */
    public function get_category_list($is_return=0){

        $category_service = new CategoryService();
        $category_list = $category_service->get_category_list();

        if($is_return) return $category_list;

        if($category_list){
            returnApiSuccess(array('category_list'=>$category_list));
        }else{
            returnApiError('无数据');
        }

    }

    /**
     * 商品规格属性、属性值列表（用于商品SKU规格选择）
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param int $is_return
     * @return mixed
     */
    public function get_attr_list($is_return=0){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        if(empty($seller_id) || empty($store_id)){
            returnApiError('参数有误');
        }

        $attr_service = new AttrService();
        $store_model = new WmStoreModel();

        //获取店铺运营分类id
        $classify_id = $store_model->get_parent_classify_id($seller_id, $store_id);

        //主属性
        $attr_list['main_attr_list'] = $attr_service->get_attr_list($classify_id, $store_id, 1);

        //次属性
        $attr_list['sec_attr_list'] = $attr_service->get_attr_list($classify_id, $store_id, 2);

        if($is_return) return $attr_list;

        if($attr_list){
            returnApiSuccess($attr_list);
        }else{
            returnApiError('无数据');
        }

    }

    /**
     * 商品规格属性值添加
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param int attr_type 属性类型（1：主要属性，2：次要属性）
     * @param string attr_value 属性值名称
     */
    public function add_attr_vlaue(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        $attr_type = I('post.attr_type', 0, 'int');
        $attr_value = I('post.attr_value');

        if(empty($seller_id) || empty($store_id) || empty($attr_value) || empty($attr_type)){
            returnApiError('参数有误');
        }
        if(!in_array($attr_type, array(1,2))){
            returnApiError('参数有误');
        }

        $attr_service = new AttrService();

        $add_attr_state = $attr_service->add_attr_vlaue($seller_id, $store_id, $attr_type, $attr_value);

        if($add_attr_state){
            returnApiSuccess(array('attr_value_id'=>$add_attr_state), '添加成功');
        }else{
            returnApiError('添加失败');
        }

    }

    /**
     * 添加商品
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param string goods_name 商品名称
     * @param int cat_id 商品分类id
     * @param int menu_id 菜单id
     * @param int unit_id 商品单位id
     * @param string goods_img 商品图片
     * @param int sort 排序
     * @param int goods_price 商品价格
     * @param int state 商品状态说明：0：未上架  1：上架 2：下架  3：平台强制下架
     * @param string describe 商品描述
     * @param json sku_json 商品sku josn组
     * @param goods_weight 商品重量
     */
    public function add_goods(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        $goods_name = I('post.goods_name');
        $cat_id = I('post.cat_id', 0, 'int');
        $menu_id = I('post.menu_id', 0, 'int');
        $unit_id = I('post.unit_id', 0, 'int');
        $goods_img = I('post.goods_img');
        $sort = I('post.sort', 0, 'int');
        $goods_price = I('post.goods_price');
        $state = I('post.state', 0, 'int');
        $describe = I('post.describe');
        $goods_weight = I('goods_weight');
        $sku_json = json_decode($_POST['sku_json'], true);
        $sku_json = $sku_json ? $sku_json : array();

        if(empty($goods_img) || empty($seller_id) || empty($store_id) || empty($goods_name) || empty($cat_id) || empty($menu_id) || empty($unit_id)){
            returnApiError('参数有误');
        }

        if(abslength($goods_name) > 30){
            returnApiError('商品名称不能超过30位数');
        }
        if(!in_array($state, array(0,1))){
            returnApiError('参数有误');
        }

        $goods_model = new SellerGoodsService();
        $add_goods = $goods_model->add_goods($seller_id, $store_id, $goods_name, $cat_id, $menu_id, $goods_price, $unit_id, $goods_img, $state, $sort, $describe, $goods_weight, $sku_json);

        if($add_goods){
            returnApiSuccess(array('goods_id'=>$add_goods), '操作成功');
        }else{
            returnApiError('操作失败');
        }

    }

    /**
     * 编辑商品
     * Created By caicm
     * @param int goods_id 商品id
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param string goods_name 商品名称
     * @param int cat_id 商品分类id
     * @param int menu_id 菜单id
     * @param int unit_id 商品单位id
     * @param string goods_img 商品图片
     * @param int sort 排序
     * @param int goods_price 商品价格
     * @param int state 商品状态说明：0：未上架  1：上架 2：下架  3：平台强制下架
     * @param string describe 商品描述
     * @param json sku_json 商品sku josn组
     * @param goods_weight 商品重量
     */
    public function edit_goods(){
        $goods_id = I('post.goods_id', 0, 'int');
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        $goods_name = I('post.goods_name');
        $cat_id = I('post.cat_id', 0, 'int');
        $menu_id = I('post.menu_id', 0, 'int');
        $unit_id = I('post.unit_id', 0, 'int');
        $goods_img = I('post.goods_img');
        $sort = I('post.sort', 0, 'int');
        $goods_price = I('post.goods_price');
        $state = I('post.state', 0, 'int');
        $describe = I('post.describe');
        $goods_weight = I('goods_weight');
        $sku_json = json_decode($_POST['sku_json'], true);
        $sku_json = $sku_json ? $sku_json : array();

        if(empty($goods_id) || empty($seller_id) || empty($store_id) || empty($goods_name) || empty($cat_id) || empty($menu_id) || empty($unit_id)){
            returnApiError('参数有误');
        }
        if(abslength($goods_name) > 30){
            returnApiError('商品名称不能超过30位数');
        }
        if(!in_array($state, array(0,1))){
            returnApiError('参数有误');
        }

        $goods_service = new SellerGoodsService();
        $goods_model = new WmGoodsModel();

        //商品详情
        $goods_info = $goods_model->find_goods_info($seller_id, $store_id, $goods_id);
        if(!$goods_info){
            returnApiError('商品不存在');
        }
        if($state==1 && $goods_info['state'] == 3){
            returnApiSuccess('商品已被平台下架不能上架');
        }

        $edit_goods_state = $goods_service->edit_goods($goods_id, $seller_id, $store_id, $goods_name, $cat_id, $menu_id, $goods_price, $unit_id, $goods_img, $state, $sort, $describe, $sku_json, $goods_weight);
        if($edit_goods_state){
            returnApiSuccess(array('goods_id'=>$goods_id), '操作成功');
        }else{
            returnApiError('操作失败');
        }


    }

    /**
     * 添加商品-所需参数
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     */
    public function get_add_goods_parameter(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        if(empty($seller_id) || empty($store_id)){
            returnApiError('参数有误');
        }

        $unit_model = new WmUnitModel();
        $menu_model = new WmStoreCustomizeMenuModel();

        $parameter_list = $this->get_attr_list(1); //属性列表
        $parameter_list['menu_list'] = $menu_model->get_menu_list($seller_id, $store_id, 0);; //自定义菜单列表
        if(!$parameter_list['menu_list']){
            returnApiError('请先添加菜单分组');
        }

        $parameter_list['category_list'] = $this->get_category_list(1); //商品分类
        $parameter_list['unit_list'] = $unit_model->get_unit_list();  //商品单位列表

        if($parameter_list){
            returnApiSuccess($parameter_list);
        }else{
            returnApiError('无数据');
        }

    }

    /**
     * 编辑商品-商品详情
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param int goods_id 商品id
     */
    public function get_edit_goods_parameter(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        $goods_id = I('post.goods_id', 0, 'int');
        if(empty($seller_id) || empty($store_id) || empty($goods_id)){
            returnApiError('参数有误');
        }

        $unit_model = new WmUnitModel();
        $menu_model = new WmStoreCustomizeMenuModel();
        $goods_model = new WmGoodsModel();
        $attr_service = new AttrService();
        $goods_service = new SellerGoodsService();

        //商品详情
        $goods_info = $goods_model->find_goods_info($seller_id, $store_id, $goods_id);
        if(!$goods_info){
            returnApiError('商品不存在');
        }
        $parameter_list = $goods_info;

        if($goods_info['is_sku']){
            $parameter_list['goods_sku'] = $goods_service->get_edit_goods_sku($goods_id);
        }else{
            $parameter_list['goods_sku'] = array();
        }

        //属性列表
        $parameter_list['attr_list'] = $attr_service->get_edit_goods_attr($this->get_attr_list(1), $goods_id); //属性列表
        $parameter_list['category_list'] = $this->get_category_list(1); //商品分类
        $parameter_list['unit_list'] = $unit_model->get_unit_list();  //商品单位列表
        $parameter_list['menu_list'] = $menu_model->get_menu_list($seller_id, $store_id, 0);; //自定义菜单列表

        if($parameter_list){
            returnApiSuccess($parameter_list);
        }else{
            returnApiError('无数据');
        }

    }

    /**
     * 获取店铺分类商品列表
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     */
    public function get_cat_goods_list(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        if(empty($seller_id) || empty($store_id)){
            returnApiError('参数有误');
        }

        $goods_service = new SellerGoodsService();

        $goods_list = $goods_service->get_cat_goods_list($seller_id, $store_id);

        if($goods_list){
            returnApiSuccess(array('menu_list'=>$goods_list));
        }else{
            returnApiError('无数据');
        }

    }

    /**
     * 商品状态修改
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param int goods_id 商品id
     * @param int state 状态 1：上架 2：下架 3:删除
     */
    public function update_goods_state(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        $goods_id = I('post.goods_id', 0, 'int');
        $state = I('post.state', 0, 'int');
        if(empty($seller_id) || empty($store_id) || empty($goods_id)){
            returnApiError('参数有误');
        }
        if(!in_array($state, array(1,2,3))){
            returnApiError('参数有误');
        }

        $goods_service = new SellerGoodsService();

        $update_state = $goods_service->update_goods_state($seller_id, $store_id, $goods_id, $state);

        if($update_state){
            returnApiSuccess(null, '操作成功');
        }else{
            returnApiError('操作失败');
        }



    }









}